package com.tech.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
	private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);

	public static String md5(String str) {
		StringBuilder builder = new StringBuilder();
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.reset();
			byte[] ba = md5.digest(str.getBytes("UTF-8"));
			for (byte b : ba) {
				if (Integer.toHexString(0xFF & b).length() == 1)
					builder.append("0").append(Integer.toHexString(0xFF & b));
				else
					builder.append(Integer.toHexString(0xFF & b));
			}
			return builder.toString();
		} catch (NoSuchAlgorithmException e) {
			LOG.error("MD5Utils NoSuchAlgorithmException :{}", e.toString());
			throw new RuntimeException(e);
		} catch (UnsupportedEncodingException e) {
			LOG.error("MD5Utils UnsupportedEncodingException :{}", e.toString());
			throw new RuntimeException(e);
		}
	}
}
